/**
 * entrance of Lite
 */
function Lite(_context_path) {
    // initial parameters here
    this.litejs_context_path = _context_path || "";
}

/**
 *
 * @type {{addScript: (function(*=, *): HTMLScriptElement), use: Lite.use, addCSS: (function(*=, *): HTMLLinkElement), getCmp: Lite.getCmp}}
 */
Lite.prototype = {
    use: function (module_name_list, callback) {
        for (var module_index in module_name_list) {
            var moduleLst = this.getCmp(module_name_list[module_index]);
            var childDom;
            for (var i in moduleLst) {
                var strPartArr = moduleLst[i].split(".");
                var resourcePath = this.litejs_context_path + module_name_list[module_index] + "/" + moduleLst[i];
                if ("js" === strPartArr[strPartArr.length - 1]) {
                    childDom = this.addScript("head", resourcePath);
                } else if ("css" === strPartArr[strPartArr.length - 1]) {
                    childDom = this.addCSS("head", resourcePath);
                }
            }
            if (module_index == module_name_list.length - 1) {
                var self = this;
                setTimeout(function () {
                    self.doCallback(childDom, callback)
                }, 100);
            }
        }
    },
    addScript: function (parent, path) {
        var pDom = document.getElementsByTagName(parent)[0];
        var cDom = document.createElement("script");
        cDom.type = "text/javascript";
        cDom.src = path;
        pDom.appendChild(cDom);
        return cDom;
    },
    addCSS: function (parent, path) {
        var pDom = document.getElementsByTagName(parent)[0];
        var cDom = document.createElement("link");
        cDom.type = "text/css";
        cDom.rel = "stylesheet";
        cDom.href = path;
        pDom.appendChild(cDom);
        return cDom;
    },
    getCmp: function (param) {
        if (ElComponent.hasOwnProperty(param)) {
            return ElComponent[param];
        }
    },
    doCallback: function (childDom, callback) {
        if ("text/css" === childDom.type) {
            styleOnload(childDom, function () {
                if (callback) {
                    callback();
                }
            });
        } else if ("text/javascript" === childDom.type) {
            childDom.onload = function () {
                if (callback) {
                    callback();
                }
            };
        }
    },
    appendSource: function (absSourceArr, callback) {
        var childDom;
        for(var k in absSourceArr){
            var resourcePath = absSourceArr[k];
            if ("js" === resourcePath.slice(-2)) {
                childDom = this.addScript("head", resourcePath);
            } else if ("css" === resourcePath.slice(-3)) {
                childDom = this.addCSS("head", resourcePath);
            }
            if (k == absSourceArr.length - 1) {
                var self = this;
                setTimeout(function () {
                    self.doCallback(childDom, callback);
                }, 100);
            }
        }
    }
};

/**
 * define the packages structure
 */
var ElComponent = {
    "UI": ["UI.css"],
    "core": ["Observer.js", "Watcher.js", "Compile.js", "EDAO.js", "ECtrl.js"],
    "move": ["mover.js"],
    "spagination": ["spagination.css", "spagination.js"],
    "http": ["http.js"]
};

/**
 * sea.js
 */
function styleOnload(node, callback) {
    // for IE6-9 and Opera
    if (node.attachEvent) {
        node.attachEvent('onload', callback);
        // NOTICE:
        // 1. "onload" will be fired in IE6-9 when the file is 404, but in
        // this situation, Opera does nothing, so fallback to timeout.
        // 2. "onerror" doesn't fire in any browsers!
    }
    // polling for Firefox, Chrome, Safari
    else {
        setTimeout(function () {
            poll(node, callback);
        }, 0); // for cache
    }
}

function poll(node, callback) {
    if (callback.isCalled) {
        return;
    }
    var isLoaded = false;
    if (/webkit/i.test(navigator.userAgent)) {//webkit
        if (node['sheet']) {
            isLoaded = true;
        }
    }
    // for Firefox
    else if (node['sheet']) {
        try {
            if (node['sheet'].cssRules) {
                isLoaded = true;
            }
        } catch (ex) {
            // NS_ERROR_DOM_SECURITY_ERR
            if (ex.code === 1000) {
                isLoaded = true;
            }
        }
    }
    if (isLoaded) {
        // give time to render.
        setTimeout(function () {
            callback();
        }, 1);
    } else {
        setTimeout(function () {
            poll(node, callback);
        }, 1);
    }
}